function Branch(name,commit){
    this.name=name;
    this.commit=commit;
}
function Commit(id,parent,message){
    this.id=id;
    this.message=message;
    this.parent=parent;
}
function Git(name){
    this.name=name;
    this.lastcommitId=-1;
    this.branches=[];
    this.HEAD=null;
    var master = new Branch("master",null);
    this.branches.push(master);
    this.HEAD=master;
}
Git.prototype.commit=function(message){
    var commit=new Commit(++this.lastcommitId,this.HEAD.commit,message);
    this.HEAD.commit=commit;
    return commit;
}
// 指针指向的链表

Git.prototype.log=function(){
    var commit=this.HEAD.commit,history=[];
    while(commit){
        history.push(commit);
        commit=commit.parent;
    }
    // 1. start from last commit 
    // 2. go back tracing to the first commit
    // 3. push in history
    return history;
}
Git.prototype.checkout=function(branchName){
    for (let i= this.branches.length;i--;){
        if(this.branches[i].naem==branchName){
            console.log('Switches to existing branch:'+branchName);
            this.HEAD=this.branches[i];
            return this;
        }
    }
    var newBranch=new Branch(branchName,this.HEAD.commit);
    this.branches.push(newBranch);
    console.log('Switched to new branch:'+ branchName);
    return this;
}
// 提交代码，生成文件的版本
const repo=new Git('my');
console.log(repo.commit('make commit work'));
repo.commit('change 1');
repo.commit('change 2');
console.log(repo.log());